/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Parents;
import java.sql.*;


/**
 *
 * @author mishra
 */
public class Query {
 static Connection con=null;
 public static int add(ParentsBean pb) throws SQLException
 {
     int i=0,k=0;
     try
     {
         con=DBConnection.Connection.getConnection();
         PreparedStatement pre=con.prepareStatement("insert into parents values(parentsid.nextval,?,?,?,?,?,?,?,?,?,?,sysdate,?,?,?)");
         pre.setString(1, pb.getFatherName());
         pre.setString(2, pb.getFatherMobileNo());
         pre.setString(3, pb.getMotherName());
         pre.setString(4, pb.getMotherMobileNo());
         pre.setString(5, pb.getFatherOccupation());
         pre.setString(6, pb.getMotherOccupation());
         pre.setString(7, pb.getGuardianName());
         pre.setString(8, pb.getRelation());
         pre.setString(9, pb.getGuardianOccupation());
         pre.setString(10, pb.getGuardianMobileNo());
         pre.setInt(11, pb.getAddressId());
         pre.setInt(12, pb.getAdmissionId());
         pre.setString(13, pb.getEntryEmail());
         i=pre.executeUpdate();
         if(i!=0)
         {
             k=getMaxId();
         }
     }catch(Exception e)
     {
         System.out.println(e);
     }
     finally{
         con.close();
     }
     return k;
 }
public static int getMaxId() throws SQLException
{
    int i=0;
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select max(parents_id) from parents");
       ResultSet rs= pre.executeQuery();
        if(rs.next())
        {
            i=rs.getInt(1);
        }
                
        
    }catch(Exception e)
    {System.out.println(e);}
    finally{con.close();}
    return i;
    
}
public static int edit(ParentsBean pb) throws SQLException
 {
     int i=0;
     try
     {
         con=DBConnection.Connection.getConnection();
         PreparedStatement pre=con.prepareStatement("update parents set FATHER_NAME=?,FATHER_MOBILE_NO=?,MOTHER_NAME=?,MOTHER_MOBILE_NO=?,FATHER_OCCUPATION=?,MOTHER_OCCUPATION=?,GUARDIAN_NAME=?,RELATION=?,GUARDIAN_OCCUPATION=?,GUARDIAN_MOBILE_NO=?,ENTRY_DATE=sysdate,ADDRESS_ID=?,ADMISSION_ID=?,ENTRY_EMAIL=? where PARENTS_ID=?");
         pre.setString(1, pb.getFatherName());
         pre.setString(2, pb.getFatherMobileNo());
         pre.setString(3, pb.getMotherName());
         pre.setString(4, pb.getMotherMobileNo());
         pre.setString(5, pb.getFatherOccupation());
         pre.setString(6, pb.getMotherOccupation());
         pre.setString(7, pb.getGuardianName());
         pre.setString(8, pb.getRelation());
         pre.setString(9, pb.getGuardianOccupation());
         pre.setString(10, pb.getGuardianMobileNo());
         pre.setInt(11, pb.getAddressId());
         pre.setInt(12, pb.getAdmissionId());
         pre.setString(13, pb.getEntryEmail());
         pre.setInt(14, pb.getParentId());
         i=pre.executeUpdate();
         
     }catch(Exception e)
     {
         System.out.println(e);
     }
     finally{
         con.close();
     }
     return i;
 }
   public static int chkParents(int admissionId, int parentId) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from PARENTS where ADMISSION_ID=? or PARENTS_ID=?");
            pre.setInt(1, admissionId);
            pre.setInt(2, parentId);
            ResultSet rs = pre.executeQuery();
            if (rs.next()) {
                i = 1;
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }
}
